﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using BLL;
using System.Data;
using Common;


namespace School.UserCentents
{
    public partial class AddArticle1 : PageBase
    {
        WenZhang bll = new WenZhang();
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["action"] = Request["action"];

            if (!this.IsPostBack)
            {
                InitDropDownList();
                this.txtAddDate.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒");
                if (Request["action"] == "3")
                {
                    int id =MyCommon.ConvertToInt(Request["id"]);
                    InitEdit(id);
                }
            }
            
            AddArticle(HttpContext.Current.Request);
        }

        private void AddArticle(HttpRequest req)
        {
            Model.WenZhang art = new Model.WenZhang();
            art.Title = this.txtTitle.Text;
            art.Contents = this.txtContent.Text;
            art.DateTime = Convert.ToDateTime(this.txtAddDate.Text);
            art.ArticleType = MyCommon.ConvertToInt(this.ddlType.SelectedValue);
            art.IsComment = this.encmt.Checked?1:2;
            art.UserID = base.LoginUserId;
            art.ID = MyCommon.ConvertToInt(req["id"]);
            
            int action = MyCommon.ConvertToInt(req["hidAction"]);

            switch (action)
            {
                case (int)ActionEnum.添加:
                    if (bll.Add(art) > 0)
                    {
                        ScriptHelper.AlertRedirect("添加文章成功！", "Article.aspx");
                    }
                    break;
                case (int)ActionEnum.修改:
                    art.EditTime = DateTime.Now;
                    if (this.IsPostBack && bll.Update(art))
                    {
                        ScriptHelper.AlertRedirect("修改文章成功！", "Article.aspx");
                    }
                    break;
            }
        }

        private void InitEdit(int id)
        {
           Model.WenZhang model = bll.GetModel(id);
           if (model != null)
           {
               this.txtTitle.Text = model.Title;
               this.txtContent.Text = model.Contents;
               this.txtAddDate.Text = model.DateTime.ToString();
               this.ddlType.SelectedValue = model.ArticleType.ToString();
               this.encmt.Checked = model.IsComment==1?true:false;
           }
        }

        private void InitDropDownList()
        {
            DataTable dt = bll.GetArticlTYpe();
            if (dt != null)
            {
                this.ddlType.DataSource = dt;
                this.ddlType.DataTextField = "name";
                this.ddlType.DataValueField = "id";
                this.ddlType.DataBind();
            }
        }
    }
}